package com.igeek.collateral.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.collateral.entity.Agency;
import com.igeek.collateral.entity.Collateral;
import com.igeek.collateral.entity.Member;
import com.igeek.collateral.mapper.MemberMapper;
import com.igeek.collateral.service.IMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.collateral.utils.QiniuUtils;
import com.igeek.collateral.vo.PageQueryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * <p>
 * 移动端会员 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-04
 */
@CacheConfig(cacheNames = "member")
@Slf4j
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

@Autowired
private MemberMapper memberMapper;

    @Transactional
    @Override
    public Page<Member> findPage(PageQueryVO vo) {
        Integer currentPage = vo.getCurrentPage();
        Integer pageSize = vo.getPageSize();
        String queryString = vo.getQueryString();
        Page<Member> page=new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<Member> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.hasLength(queryString),Member::getName,queryString);
        Page<Member>memberPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        return memberPage;
    }

    @Transactional
    @Override
    public boolean delete(Long id) {
        LambdaQueryWrapper<Member> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Member::getId,id);
        int i=this.baseMapper.delete(lambdaQueryWrapper);
        return i>0?true:false;
    }

    public String uploadByQiniuyun(MultipartFile imgFile) throws IOException {
        if(imgFile!=null){
            String oldName = imgFile.getOriginalFilename();
            if(StringUtils.hasLength(oldName)){
                String newName = UUID.randomUUID()+oldName;
                //上传至七牛云服务器
                InputStream inputStream = imgFile.getInputStream();
                String pic = QiniuUtils.uploadForQiniu(inputStream, newName);
                return pic;
            }
        }
        return null;
    }
@Cacheable(key = "#id")
    @Override
    public Member findOneById(Long id) {
        Member member = this.baseMapper.selectById(id);
        return member;
    }

    @Override
    public boolean updatePasswordById(Long id) {
        LambdaQueryWrapper<Member> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Member::getId,id);
        int i = baseMapper.update(null, lambdaQueryWrapper);
        return i>0?true:false;

}

}
